package com.hqyj.hrms.modules.salary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.hrms.modules.attendance.entity.Attendance;
import com.hqyj.hrms.modules.salary.dao.SalaryMapper;
import com.hqyj.hrms.modules.salary.entity.Salary;
import com.hqyj.hrms.modules.salary.service.SalaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.Record;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-07
 */
@Service
public class SalaryServiceImpl  implements SalaryService {
    @Autowired(required = false)
    private SalaryMapper salaryMapper;

    @Override
    public HashMap<String, Object> add(Salary salary) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = salaryMapper.insert(salary);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","新增失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Salary salary) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = salaryMapper.updateById(salary);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer salId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = salaryMapper.deleteById(salId);
        if(num>0){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> list(Salary salary) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String keyWord = salary.getKeyWord();
        QueryWrapper wrapper = new QueryWrapper<Record>()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "sal_id", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_id", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_name", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "basic_salary", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "performance_salary", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "bonus", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "penalty", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "month", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "total_sal", keyWord
                );
        Page<Salary> salaryPage = new Page<>(salary.getPage(),salary.getRow());


        Page<Salary> pageList = salaryMapper.selectPage(salaryPage,wrapper);
        map.put("info",pageList.getRecords());
        //获取总条数
        map.put("total",pageList.getTotal());
        //获取总页数
        map.put("totalPage",pageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> selectByMonth2(String month) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Map<String,Object> params = new HashMap<>();
        params.put("month",month);
        QueryWrapper<Salary>wrapper = new QueryWrapper<>();
        wrapper.allEq(params);
        Page<Salary> salaryPage = new Page<>(1,5);
        Page<Salary> pageList = salaryMapper.selectPage(salaryPage,wrapper);
        map.put("info",pageList.getRecords());
        //获取总条数
        map.put("total",pageList.getTotal());
        //获取总页数
        map.put("totalPage",pageList.getPages());
        return map;

    }



    @Override
    public List<Salary> totalNum(Salary salary) {


        List<Salary> salaries = salaryMapper.totalNum(salary);
        salaryMapper.updateById(salaries.get(0));
          return salaries;
    }

    @Override
    public List<Map> chart() {
        List<Salary> list=new ArrayList();
        list=salaryMapper.getBonus();
        List<Map> maps=new ArrayList<Map>();
        for (int i=0;i<list.size();i++){
            String empName = list.get(i).getEmpName();
            Integer bonus=list.get(i).getBonus();
            Map map=new HashMap();
            map.put("name",empName);
            map.put("value",bonus);
            maps.add(map);
        }
        return maps;
    }

    @Override
    public List<Map> chart2() {
        List<Salary> list=new ArrayList();
        list=salaryMapper.getPenalty();
        List<Map> maps=new ArrayList<Map>();
        for (int i=0;i<list.size();i++){
            String empName = list.get(i).getEmpName();
            Integer penalty=list.get(i).getPenalty();
            Map map=new HashMap();
            map.put("name",empName);
            map.put("value",penalty);
            maps.add(map);
        }
        return maps;
    }
    }




